package com.weizhong.update.appdb;

import java.util.ArrayList;
import java.util.List;

import com.weizhong.update.model.AppEntity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.nfc.Tag;
import android.util.Log;

public class DBManager {
	private static final String TAG = "DBManager";
	private DBHelper helper;
	private SQLiteDatabase db;
	
	public DBManager(Context context) {
		helper = new DBHelper(context);
		//因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
		//所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
		db = helper.getWritableDatabase();
	}
	
	/**
	 * add appEntitys
	 * @param appEntitys
	 */
	public void add(List<AppEntity> appEntitys) {
		
        db.beginTransaction();	//开始事务
        try {
        	for (AppEntity appEntity : appEntitys) {
        		Log.d(TAG, "appEntity : " + " package: " +   appEntity.getPackageName());
        		db.execSQL("INSERT INTO appentity VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        				new Object[]{appEntity.getId(), appEntity.getAppName(), appEntity.getPackageName(),
        						String.valueOf(appEntity.getApkSize()),appEntity.getApkPath(),appEntity.getVerName(),
        						String.valueOf(appEntity.getVerCode()),appEntity.getAction(),
        						appEntity.getUpdateDes(),appEntity.getAppType(),appEntity.getIsDeskTop(),
        						appEntity.isDownloadSuccess(),appEntity.isUpdateSuccess(),-1});
        	}
        	db.setTransactionSuccessful();	//设置事务成功完成
        } finally {
        	db.endTransaction();	//结束事务
        }
	}
	
	/**
	 * update AppEntity updateDownloadResult
	 * @param appEntity
	 */
	public void updateDownloadResult(AppEntity appEntity) {
		ContentValues cv = new ContentValues();
		cv.put("downloadSuccess", appEntity.isDownloadSuccess());
		db.update("appentity", cv, "packageName = ?", new String[]{appEntity.getPackageName()});
	}
	
	/**
	 * update AppEntity updateUpdateResult
	 * @param appEntity
	 */
	public void updateUpdateResult(AppEntity appEntity) {
		Log.d(TAG, "updateUpdateResult: " + appEntity.getAppName() + ",download result: " + appEntity.isUpdateSuccess());
		ContentValues cv = new ContentValues();
		cv.put("updateSuccess", appEntity.isUpdateSuccess());
		db.update("appentity", cv, "packageName = ?", new String[]{appEntity.getPackageName()});
	}
	
	/**
	 * update AppEntity updateDownloadID
	 * @param appEntity
	 */
	public void updateDownloadID(AppEntity appEntity) {
		ContentValues cv = new ContentValues();
		cv.put("downloadid", appEntity.getDownloadID());
		db.update("appentity", cv, "packageName = ?", new String[]{appEntity.getPackageName()});
	}
	
	/**
	 * update AppEntity updateDownloadID
	 * @param appEntity
	 */
	public void updateDeskTop(AppEntity appEntity) {
		ContentValues cv = new ContentValues();
		cv.put("isDeskTop", appEntity.getIsDeskTop());
		db.update("appentity", cv, "packageName = ?", new String[]{appEntity.getPackageName()});
	}
	
	/**
	 * delete  appEntity
	 * @param person
	 */
	public void deleteAppEntity(AppEntity appEntity) {
		db.delete("appentity", "packageName = ?", new String[]{appEntity.getPackageName()});
	}
	
	public void deleteAllAppEntity(){
		Log.i(TAG, "deleteAllAppEntity");
		db.execSQL("DELETE FROM appentity");
	}
	/**
	 * query all persons, return list
	 * @return List<Person>
	 */
/*	public List<AppEntity> query() {
		ArrayList<AppEntity> persons = new ArrayList<AppEntity>();
		Cursor c = queryTheCursor();
        while (c.moveToNext()) {
        	Person person = new Person();
        	person._id = c.getInt(c.getColumnIndex("_id"));
        	person.name = c.getString(c.getColumnIndex("name"));
        	person.age = c.getInt(c.getColumnIndex("age"));
        	person.info = c.getString(c.getColumnIndex("info"));
        	persons.add(person);
        }
        c.close();
        return persons;
	}*/
	
	/**
	 * query all persons, return cursor
	 * @return	Cursor
	 */
	public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT * FROM appentity", null);
        return c;
	}
	
	public Cursor queryPackageCursor(String packageName){
		Cursor cursor = db.rawQuery("SELECT * FROM appentity WHERE packageName = ?", new String[]{packageName});
		return cursor;
	}
	
	/**
	 * close database
	 */
	public void closeDB() {
		db.close();
	}
}